//
//  ZYLTabBarButton.m
//  XXWeiBo
//
//  Created by mac on 15-1-31.
//  Copyright (c) 2015年 mac. All rights reserved.
//

#import "ZYLTabBarButton.h"

@implementation ZYLTabBarButton

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        [self setup];
    }
    return self;
}

- (void)setup
{
    self.imageView.contentMode = UIViewContentModeCenter;
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.titleLabel.font = [UIFont systemFontOfSize:13];
    [self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
}


- (CGRect)imageRectForContentRect:(CGRect)contentRect
    {
        CGFloat imageW = contentRect.size.width;
        CGFloat imageH = contentRect.size.height * 0.6;
        CGFloat imageX = 0;
        CGFloat imageY = 0;
        return CGRectMake(imageX, imageY, imageW, imageH);
    }
    
    - (CGRect)titleRectForContentRect:(CGRect)contentRect
    {
#warning 如果相对自定义按钮通过transform进行缩放, 那么不能在自定义按钮中访问self.frame
        // Y = 按钮的高度 - 图片的高度
        CGFloat titleY = contentRect.size.height* 0.6;
        CGFloat titleW = contentRect.size.width;
        CGFloat titleH = contentRect.size.height - contentRect.size.height * 0.6;
        CGFloat titleX = 0;
        return CGRectMake(titleX, titleY, titleW, titleH);
    }
    
    
    - (void)setItem:(UITabBarItem *)item
    {
        _item = item;
        [self setTitle:item.title forState:UIControlStateNormal];
        [self setImage:item.image forState:UIControlStateNormal];
        [self setImage:item.selectedImage forState:UIControlStateSelected];
    }

- (void)setHighlighted:(BOOL)highlighted
{
    
}
@end
